package com.itheima.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.tanhua.domain.Question;
import com.itheima.tanhua.domain.Settings;
import com.itheima.tanhua.domain.User;
import com.itheima.tanhua.domain.UserInfo;
import com.itheima.tanhua.dubbo.dao.QuestionMapper;
import com.itheima.tanhua.dubbo.dao.SettingsMapper;
import com.itheima.tanhua.dubbo.dao.UserInfoMapper;
import com.itheima.tanhua.dubbo.dao.UserMapper;
import com.itheima.tanhua.po.RecommendationPo;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.swing.text.DateFormatter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author itheima
 * @since 2022-01-13
 */
@DubboService
public class UserClient implements UserApi {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private SettingsMapper settingsMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public User findByPhone(String phone) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(phone), User::getMobile, phone);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public User createOne(String phone) {
        User user = new User();
        user.setMobile(phone);
        user.setPassword("123456");
        user.setCreated(LocalDateTime.now());
        int insert = userMapper.insert(user);
        return insert > 0 ? user : null;
    }

    @Override
    public Boolean addUserInfo(UserInfo userInfo) {
        if (Objects.isNull(userInfo)) {
            return false;
        }

        userInfo.setCreated(LocalDateTime.now());
        return userInfoMapper.insert(userInfo) > 0;
    }

    @Override
    public Boolean updateUserInfo(UserInfo userInfo) {
        userInfo.setUpdated(LocalDateTime.now());
        return userInfoMapper.updateById(userInfo) > 0;
    }

    @Override
    public UserInfo findUserInfoByUserId(String userId) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(userId), UserInfo::getId, userId);
        return userInfoMapper.selectOne(wrapper);
    }

    @Override
    public UserInfo getRecommendUser(String userId) {
        return null;
    }

    @Override
    public Settings findSettingsByUserId(String userId) {
        LambdaQueryWrapper<Settings> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Settings::getUserId, userId);
        return settingsMapper.selectOne(wrapper);
    }

    @Override
    public Boolean updateSettingByUserId(Settings settings) {
        LambdaQueryWrapper<Settings> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Settings::getUserId, settings.getUserId());
        return settingsMapper.update(settings, wrapper) > 0;
    }

    @Override
    public Boolean createQuestion(Question question) {
        return questionMapper.insert(question) > 0;
    }

    @Override
    public Boolean updateQuestionByUserId(Question question) {
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Question::getUserId, question.getUserId());
        return questionMapper.update(question, wrapper) > 0;
    }

    @Override
    public Question findQuestionByUserId(String userId) {
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Question::getUserId, userId);
        return questionMapper.selectOne(wrapper);
    }

    @Override
    public UserInfo randomUserByCity(String userId, String city) {
        return userInfoMapper.randomUserByCity(userId, city);
    }

    @Override
    public List<UserInfo> findRecommendUserList(String userId, RecommendationPo po) {
        Integer page = po.getPage();
        Integer pagesize = po.getPagesize();
        Integer skip = (page - 1) * pagesize;

        return userInfoMapper.selectUsersByCity(userId, skip, pagesize, po);
    }
}
